<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends BaseController {
    public function index(){
        $channel_id = session('channel_id');

        if(!empty($channel_id) ){
            $title = M('channel')->where('id = '.$channel_id)->field('nick_name')->find();
        }else{
            $title='';
        }

        if(empty($title)){
            $this->assign('title','369页游');
        }else{
            $this->assign('title',$title['nick_name']);
        }
        $user_id = session('user_id');
        $map = array('player_id'=>$user_id);
        $his_games = M('PlayerGame')->where($map)->order('last_play_time desc')->limit(10)->select();
        $Game = M('Game');
        $map = array('hot'=>1,'status'=>1);
        $hot_list = $Game->where($map)->order('order_number desc,add_time desc')->select();
        $banner = array();
        if (!empty($hot_list)) {
            foreach ($hot_list as $item) {
                $data = array(
                    'title' => $item['name'],
                    'subtitle' => $item['sub_title'],
                    'img'=>'../appc/Uploads/'.$item['s_pic'],
                    'url' => U('Index/detail', array('id'=>$item['id']))
                );
                $banner[] = $data;
            }
        }
        if (!empty($his_games)){
            $his_list = array();
            foreach ($his_games as $key=>$item) {
                $game = $Game->where(array('id'=>$item['game_id'],'status'=>1))->find();
                if (!empty($game)) {
                    $data = array(
                        'title' => $game['name'],
                        'subtitle' => $game['sub_title'],
                        'img'=>'../appc/Uploads/'.$game['icon'],
                        'url' => U('Index/detail', array('id'=>$game['id']))
                    );
                    $his_list[] = $data;
                }
            }
            $this->assign('his',$his_list);
        }
        $this->assign('banner',$banner);

        //获取精选
        if ($channel_id != 1) {
            $map = array('status'=>1);
        } else {
            $map = array();
        }
        $list = D('Game')->where($map)->order('order_push desc,order_number desc')->limit('0,5')->select();
        $sift = array();
        if (!empty($list)) {
            foreach ($list as $item){
                $data = array(
                    'id'=>$item['id'],
                    'title' => $item['name'],
                    'subtitle' => $item['sub_title'],
                    'img'=>'../appc/Uploads/'.$item['icon'],
                    'url' => U('Index/detail', array('id'=>$item['id']))
                );
                $sift[] = $data;
            }

        }

        //人气游戏
//        $where = array(
//            'type'=>1
//        );
        //$game_data = M('GameData')->field('game_id,sum(value) num')->group('game_id')->where($where)->select();



        
        $where = array(
            'g.status'=>1,
            'd.type'=>1
        );
        $join = 'appc_game_data d ON d.game_id = g.id';
        $hot = $Game->alias('g')->join($join)->field('g.name,icon,d.game_id,sum(d.value) num')->where($where)->order('num desc')->group('game_id')->limit('0,8')->select();
        $this->assign('hot_game',$hot);



        $this->assign('sift',$sift);
        $this->assign('type',session('type'));

            $this->display();


    }
    

    public function gameList(){
        $channel_id = session('channel_id');
        if ($channel_id != 1) {
            $map = array('status'=>1);
        } else {
            $map = array();
        }
        
        $page = I('page',1);
        $pagesize = I('pagesize', 20);
        $list = D('Game')->where($map)->order('order_number desc')->limit(($page-1) * $pagesize, $pagesize)->select();
        $rst = array();
        if (!empty($list)) {
            foreach ($list as $item){
                $data = array(
                    'id'=>$item['id'],
                    'title' => $item['name'],
                    'subtitle' => $item['sub_title'],
                    'img'=>'../appc/Uploads/'.$item['icon'],
                    'url' => U('Index/detail', array('id'=>$item['id']))
                );
                $rst[] = $data;
            }
            
        }
        $this->ajaxReturn($rst);
    }
    
    public function detail(){
        $id = I('id');
        if (empty($id)) {$this->error('error');}
        $game = D('Game')->where(array('id'=>$id))->find();
        if (empty($game)) {
            $this->error('游戏错误', U('Index/index'));
        }
        // 判断URL 是否是http 还是 https 的。
        $tmp = substr($game['url'],0,5);
        $game['url'] = rtrim($game['url']);
        $tmp = $tmp =='https' ? $tmp : 'http';
        $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https' : 'http';
        if ($tmp != $sys_protocal) {
            $url = $tmp.'://'.get_url(false);
            header('Location:'.$url);
            exit();
        }
        if ($game['gid'] == '10004') { // 空中网
            $url = getGameUrl($game['url'], $game['appkey'],$game['loginkey']);
        } else { // 其他网络，统一接口
            $url = getH5GameUrl($game['gid'],$game['id'],$game['url'],$game['appkey']);
        }
        
        $map = array('player_id'=>session('user_id'), 'game_id'=>$id);
        $PlayerGame = D('PlayerGame');
        $play = $PlayerGame->where($map)->find();
        if (empty($play)) {
            $data = $map;
            $PlayerGame->add($data);
        } else {
            $data = array('id'=>$play['id'], 'last_play_time'=>getFomartDate());
            $PlayerGame->save($data);
        }
        
        // 统计游戏数据
        $day = getFomartDate(NOW_TIME, 'd+');
        $map = array('game_id'=>$id, 'type'=>1, 'day'=>$day);
        $model = M('GameData');
        $one = $model->where($map)->find();
        if (empty($one)) {
            $data = array(
                'game_id'=>$id, 'type'=>1, 'day'=>$day,
                'value'=>1
            );
            @$model->add($data);
        } else {
            @$model->where(array('id'=>$one['id']))->setInc('value');
        }
        $openid = session('openid');
        import('@.Lib.Wx');
        $wx = new \Boo\WX(array('get' => 'userinfo'));
        $subscribe = $wx->get_user_info($openid );
        $subscribe = empty($subscribe)?0:$subscribe;
        $this->assign('subscribe',$subscribe);
        $this->assign('title',$game['name'] .'——'. $game['sub_title']);
        $this->assign('name',$game['name']);
        $this->assign('sub_title',$game['sub_title']);
        $this->assign('icon',$game['icon']);
        $this->assign('url', $url);
        $this->assign('status',$game['status']);
        $this->assign('game_id',$id);
        $this->assign('player_id',session('user_id'));
        $this->assign('type',session('type'));
        $channel_id = session('channel_id');
        $pay_type = session('pay_type');
        $channel_info = M('Channel')->where(array('id'=>$channel_id))->field('icon,ad,pay_type')->find();
        if(empty($pay_type)){
            $pay_type = $channel_info['pay_type'];
        }
        $this->assign('pay_type',$pay_type);
        $this->assign('channel_id',$channel_id);
        $this->assign('ad',$channel_info['ad']);
        $this->assign('channel_icon',$channel_info['icon']);

        //分享
        import('@.Lib.Wx');
        $api = new \Boo\WX();
        $api_ticket = $api->getjsapi_ticket();
        require_cache('./test/lib/WxPay.Api.php');
        $wx = new \WxPayJsApiPay;
        $weixing = array();
        $weixing['appid'] = \WxPayConfig::APPID;
        $weixing['timestamp'] = time();
        $weixing['nonceStr'] = \WxPayApi::getNonceStr();
        $wx->SetApiTicket($api_ticket);
        $wx->SetTimeStamp($weixing['timestamp']);
        $wx->SetNonceStr($weixing['nonceStr']);
        $wx->SetUrl($sys_protocal.'://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
        $wx->SetSigna();
        $weixing['sign'] = $wx->getSign();
        $this->assign('wx',$weixing);
        //判断是否在微信浏览器
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_agent, 'MicroMessenger') === false) {
            $is_wx = 0;
        }else{
            $is_wx = 1;
        }
        $this->assign('is_wx',$is_wx);

        $user_id = session('user_id');
        $debug_users = array('10952','10950','10020','10003','23440','11604');
        if($is_wx == 0){
            $this->display('detailChrome');
        }elseif(in_array($user_id,$debug_users)) {
            $this->display('detailTest');
        } else {
            $this->display();
        }
    }

}